﻿//一.定义全局变量命名空间
//只创建一个全局变量，并定义该变量为当前应用容器，把其他全局变量追加在该命名空间下

//var a = {
//    defaultParam: {
//        '参数1': '初始值',
//        '参数2': 0,
//        '参数3': true
//    },
//    b: function (param) {
//        if (!param) param = {};
//        if (typeof param['参数1'] === 'undefined') {
//            param['参数1'] = a.defaultParam['参数1']
//        }
//        if (typeof param['参数2'] === 'undefined') {
//            param['参数2'] = a.defaultParam['参数2']
//        }
//        if (typeof param['参数3'] === 'undefined') {
//            param['参数3'] = a.defaultParam['参数3']
//        }
//        alert(param['参数1']);
//        alert(param['参数2']);
//        //alert(param['参数3']);
//    }
//};
//namespaces.lay = {
//    school_work: "study",
//    family_work: "we are"
//};

var layer, laypage,form, JsonData = {}, EditSingle = 0;
var namespaces = namespaces || {};
layui.config({
    base: 'plugins/layui/modules/'
});

(function($) {
    //定义一些默认参数
    var options={
        'get_url': null,
        'page_url': null,
        'Array_id': 0,
        'container': 'main',
        'row': 0,
        'column': 0,
        'Array_title': null,
        'table': null,
        'pagecount': 0,
        'editMode': 'edit',
        'rowCount': 0,
        'rowNum': 10,
        'curr':1,
        'original_row': -1,
        'tr_count': 0,
        'curr_num': 0,
        'select_row': 10,
        'select_item': '*',
        'KindEditor': null,
        'selectImage': null,
        'id_selector': function (selector) {
            return document.getElementById(selector);
        }
    }
    //定义一些api
    var plugin_api = {
        init: function (param) {
            if (!param) param = {};
            if (typeof param['get_url'] !== 'undefined') { options.get_url = param['get_url']; }
            if (typeof param['page_url'] !== 'undefined') { options.page_url = param['page_url']; }
            if (typeof param['Array_id'] !== 'undefined') { options.Array_id = param['Array_id']; }
            if (typeof param['container'] !== 'undefined') { options.container = param['container']; }
            if (typeof param['row'] !== 'undefined') { options.row = param['row']; }
            if (typeof param['column'] !== 'undefined') { options.column = param['column']; }
            if (typeof param['Array_title'] !== 'undefined') { options.Array_title = param['Array_title']; }
            if (typeof param['table'] !== 'undefined') { options.table = param['table']; }
            if (typeof param['pagecount'] !== 'undefined') { options.pagecount = param['pagecount']; }
            if (typeof param['rowCount'] !== 'undefined') { options.rowCount = param['rowCount']; }
            if (typeof param['rowNum'] !== 'undefined') { options.rowNum = param['rowNum']; }
            if (typeof param['editMode'] !== 'undefined') { options.editMode = param['editMode']; }
            if (typeof param['curr'] !== 'undefined') { options.curr = param['curr']; }
            if (typeof param['tr_count'] !== 'undefined') { options.tr_count = param['tr_count']; }
            if (typeof param['curr_num'] !== 'undefined') { options.curr_num = param['curr_num']; }
            if (typeof param['select_row'] !== 'undefined') { options.select_row = param['select_row']; }
            if (typeof param['select_item'] !== 'undefined') { options.select_item = param['select_item']; }
            plugin_api.GetValue();
            //plugin_api.paging();
        },
        GetValue: function () {
            options.select_row = parseInt($("#select_row").val()) ? parseInt($("#select_row").val()) : options.rowNum;
            var sql1,sql2;
            if (options.curr == 1) {
                sql1 = "select SQL_CALC_FOUND_ROWS " + options.Array_id + " from " + options.table + " order by time DESC" + " limit 0," + options.curr * options.select_row + ";";
                sql2 = "SELECT FOUND_ROWS();";
            }
            else {
                sql1 = "select SQL_CALC_FOUND_ROWS " + options.Array_id + " from " + options.table + " order by time DESC" + " limit " + (options.curr - 1) * options.select_row + "," + options.select_row + ";";
                sql2 = "SELECT FOUND_ROWS();";
            }
            $.ajax({
                type: 'POST',
                url: options.get_url + "?t=" + Math.random().toString(),
                data: {
                    "sql1": sql1,
                    "sql2": sql2,
                    "curr": options.rowNum
                },
                dataType: 'json',
                cache: false,
                beforeSend: function () {
                    //加载中
                    //layer.msg('加载中！', { icon: 2 });
                },
                success: function (data, textStatus) {
                    parent.layer.msg('加载完毕！', { icon: 1, time: 1000 });
                    //console.log(data);
                    //var jsonData = eval(data);
                    //console.log(jsonData.row)
                    //var Array_item = [];
                    ////options.rowCount = jsonData.length;
                    //$("#" + options.container).empty();
                    //var c = 0;
                    //$("#" + options.container).append('<table id="dynamic-table" class="layui-table site-table table-hover admin-table"></table>')
                    //$("#dynamic-table").append("<thead id='title'></thead>")
                    //$("#title").append("<tr id='line'></tr>");
                    ////表头
                    //for (c; c < options.column; c++) {
                    //    if (c == 0) { $("#line").append("<th  style='text-align:center;'><input type='checkbox' lay-filter='selected-all' lay-skin='primary' id='selected-all' Dataid='" + options.Array_id[0] + "'>"); }//<input type='checkbox' id='selected-all' Dataid='"+options.Array_id[0]+"'/> 
                    //    else { $("#line").append("<th  style='text-align:center;'>" + options.Array_title[c - 1] + "</th>"); }
                    //}
                    //$("#dynamic-table").append("<tbody id='cont'></tbody")

                    //$.each(jsonData, function (index, objVal) { //遍历对象数组，index是数组的索引号，objVal是遍历的一个对象。objVal["属性"]可取到对应的属性值。 
                    //    $.each(options.Array_id, function (idx, val) {
                    //        Array_item.push(objVal[val]);
                    //    });
                    //    $("#cont").append("<tr id=r-" + index + "></tr>");
                    //    var j = 0;
                    //    for (j; j < options.column; j++) {
                    //        if (j == 0) {
                    //            $("#r-" + index).append("<td id='r-" + index + "-" + j + "'><input type='checkbox' lay-skin='primary' id=del-" + index + " Dataid=" + options.Array_id[0] + " Datavalue=" + Array_item[0] + " ></td>");
                    //        }
                    //        else {
                    //            if (j == (options.column - 1)) {

                    //                $("#r-" + index).append("<td><button class='layui-btn layui-btn-mini' title='编辑' Datavalue=" + Array_item[0] + " Dataid=" + options.Array_id[0] + " id='r-" + index + "-" + j + "'  onclick=CJPlugin.buttonStatus('r-" + index + "-" + j + "','r-" + index + "-" + (j + 1) + "','r-" + index + "-" + (j + 2) + "','r-" + index + "-" + (j + 3) + "','" + index + "','" + j + "','" + options.editMode + "');>编辑</button>"
                    //                + "<button class='layui-btn layui-btn-mini layui-btn-normal' title='更新' Datavalue=" + Array_item[0] + " Dataid=" + options.Array_id[0] + " id='r-" + index + "-" + (j + 1) + "' onclick=CJPlugin.buttonStatus('r-" + index + "-" + j + "','r-" + index + "-" + (j + 1) + "','r-" + index + "-" + (j + 2) + "','r-" + index + "-" + (j + 3) + "','" + index + "','" + j + "','update','" + options.table + "');  style='display:none;'>更新</button>"
                    //                + "<button class='layui-btn layui-btn-mini layui-btn-warm' title='取消' Datavalue=" + Array_item[0] + " Dataid=" + options.Array_id[0] + " id='r-" + index + "-" + (j + 2) + "'  onclick=CJPlugin.buttonStatus('r-" + index + "-" + j + "','r-" + index + "-" + (j + 1) + "','r-" + index + "-" + (j + 2) + "','r-" + index + "-" + (j + 3) + "','" + index + "','" + j + "','cancel');  style='display:none;'>取消</button>"
                    //                + "<button class='layui-btn layui-btn-mini layui-btn-danger' title='删除 'Datavalue=" + Array_item[0] + " Dataid=" + options.Array_id[0] + " id='r-" + index + "-" + (j + 3) + "'  onclick=CJPlugin.buttonStatus('r-" + index + "-" + j + "','r-" + index + "-" + (j + 1) + "','r-" + index + "-" + (j + 2) + "','r-" + index + "-" + (j + 3) + "','" + index + "','" + j + "','delete','" + options.table + "');>删除</button>"
                    //                + "<button class='layui-btn layui-btn-mini layui-btn-primary' title='预览' id='r-" + index + "-" + (j + 4) + "' class='btn btn-xs btn-warning' onclick=''>预览</button></td>");
                    //            }
                    //            else {
                    //                $("#r-" + index).append("<td id='r-" + index + "-" + j + "'Dataid=" + options.Array_id[j] + ">" + Array_item[j] + "</td>")
                    //            }
                    //        }
                    //    }

                    //    Array_item = [];
                    //    form.render('checkbox');
                    //});
                },
                error: function (xhr) {
                    if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { // 兼容调试时301/302重定向导致触发error的问题
                        //console.log(xhr);
                        this.success(eval('(' + xhr.responseText + ')'), xhr.statusText);
                    }
                    parent.layer.msg('加载失败', { icon: 2, time: 1000 });
                },
            });
        },
        paging: function () {
            var sql1 = "SELECT SQL_CALC_FOUND_ROWS * FROM " + options.table + " order by time DESC LIMIT 0,10;";
            var sql2 = "SELECT FOUND_ROWS();";
            $.ajax({
                type: 'POST',
                url: options.page_url + "?t=" + Math.random().toString(),
                data: {
                    "sql1": sql1,
                    "sql2": sql2
                },
                dataType: 'json',
                cache: false,
                //async: false,
                beforeSend: function () {
                    ///
                },
                success: function (data, textStatus) {
                    var datanum = parseInt(data) == 0 ? data = 1 : data;
                    options.select_row = parseInt($("#select_row").val()) ? parseInt($("#select_row").val()) : 10;
                    options.pagecount = Math.ceil(parseInt(datanum) / options.select_row);
                    //page
                    laypage({
                        cont: 'page',
                        pages: options.pagecount,//总页数 
                        groups: 3,//连续显示分页数
                        skip: true, //是否开启跳页
                        //skin: '#5FB878',
                        curr: options.curr_num || 1, //当前页
                        jump: function (obj, first) {
                            //得到了当前页，用于向服务端请求对应数据                        
                            if (!first) {
                                options.curr = obj.curr;
                                options.curr_num = obj.curr;
                                plugin_api.GetValue();
                            }
                        }
                    });
                },
                error: function (xhr) {
                    if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { // 兼容调试时301/302重定向导致触发error的问题
                        console.log(xhr);
                        this.success(eval('(' + xhr.responseText + ')'), xhr.statusText);
                        return;
                    }
                },

            });
        },
        buttonStatus: function (edit_button, update_button, cancel_button, del_button, row, column, type, tableid) {
            options.tr_count = $("#cont tr").length;
            var a = 1, b = 1, c = 1, r = 0;
            switch (type) {
                case "edit_a":
                    if (document.getElementById("r-" + row + "-1").innerHTML != "") {//数据是否为空
                        $("#" + edit_button + "").css("display", "none");
                        $("#" + del_button + "").css("display", "none");
                        $("#" + update_button + "").css("display", "");
                        $("#" + cancel_button + "").css("display", "");
                        for (c; c < column; c++) {
                            if ($("#r-" + row + "-" + c + "").attr("Dataid") != "time") {//时间字段不需要编辑
                                var value = $("#r-" + row + "-" + c + "").html();
                                $("#r-" + row + "-" + c + "").empty();
                                $("#r-" + row + "-" + c + "").attr("Datavalue", value);
                                $("#r-" + row + "-" + c + "").append("<input style='width:80%;height:100%;' type='text' onchange=CJPlugin.setJson('" + row + "','" + $("#r-" + row + "-" + c + "").attr("Dataid") + "',this.value) style='width:90%;' value='" + $("#r-" + row + "-" + c + "").attr("Datavalue") + "'/>");
                            } else { break; }
                        }
                        if (options.original_row != row) {
                            for (a; a < column; a++) {
                                if (a != row) {
                                    $("#r-" + options.original_row + "-" + column + "").css("display", "");
                                    $("#r-" + options.original_row + "-" + (Number(column) + 1) + "").css("display", "none");
                                    $("#r-" + options.original_row + "-" + (Number(column) + 2) + "").css("display", "none");
                                    $("#r-" + options.original_row + "-" + (Number(column) + 3) + "").css("display", "");
                                }
                            }
                            for (b; b < column; b++) {
                                if ($("#r-" + row + "-" + b + "").attr("Dataid") != "time") {//时间字段不需要编辑
                                    $("#r-" + options.original_row + "-" + b + "").empty();
                                    $("#r-" + options.original_row + "-" + b + "").append($("#r-" + options.original_row + "-" + b + "").attr("Datavalue"));
                                } else { break; }
                            }
                            //纪录当前行
                            options.original_row = row;
                        }
                    }
                    else { parent.layer.msg('无数据', { icon: 2, time: 2000 }); }
                    break;
                case "edit_b":
                    if (document.getElementById("r-" + row + "-1").innerHTML != "") {//数据是否为空
                        //弹出即全屏
                        var index = parent.layer.open({
                            type: 2,
                            maxmin: true,
                            scrollbar: false,
                            shade: [0.2],
                            title: "新闻编辑",
                            area: ['800px', '600px'],
                            //btn: ['火速围观', '残忍拒绝'],
                            //moveType: 1, //拖拽模式，0或者1
                            content: '/master/news-edit.html?id=' + $("#del-" + row).attr("datavalue") + '&file=' + $("#r-" + row + "-4").text(),
                        });
                        parent.layer.full(index);
                    }
                    else { parent.layer.msg('无数据', { icon: 2, time: 2000 }); }
                    break;
                case "edit_c":
                    mid = $("#del-" + row).attr("datavalue");
                    parent.layer.open({
                        type: 2,
                        scrollbar: false,
                        shade: [0.2],
                        shadeClose: false,
                        area: ['460px', '450px'],
                        //btn: ['火速围观', '残忍拒绝'],
                        //moveType: 1, //拖拽模式，0或者1
                        content: '/master/edit-user.html?mid=' + mid + "&idx=" + row + "&idList=" + options.Array_id,

                    });
                    break;
                case "cancel":
                    for (r; r < options.tr_count; r++) {
                        $("#r-" + r + "-" + column + "").css("display", "");
                        $("#r-" + r + "-" + (Number(column) + 1) + "").css("display", "none");
                        $("#r-" + r + "-" + (Number(column) + 2) + "").css("display", "none");
                        $("#r-" + r + "-" + (Number(column) + 3) + "").css("display", "");
                    }
                    for (c; c < column; c++) {
                        if ($("#r-" + row + "-" + c + "").attr("Dataid") != "time") {//时间字段不需要编辑
                            $("#r-" + row + "-" + c + "").empty();
                            $("#r-" + row + "-" + c + "").append($("#r-" + row + "-" + c + "").attr("Datavalue"));
                        } else { break; }
                    }
                    break;
                case "update":
                    //数据库操作
                    var sqlstring = null;
                    $.each(JsonData, function (idx, val) {//迭代jsondate 为空不执行。
                        sqlstring = "UPDATE " + tableid + " SET";
                        $.each(JsonData, function (name, value) {
                            sqlstring = sqlstring + " " + name + "='" + value + "',";
                        });
                    });
                    if (sqlstring != null) {
                        sqlstring = sqlstring.substring(0, sqlstring.length - 1) + " WHERE " + $("#del-" + row).attr("Dataid") + " ='" + $("#del-" + row).attr("Datavalue") + "'";
                        $.ajax({                                                  //调用jquery的ajax方法   
                            type: "POST",                                      //设置ajax方法提交数据的形式   
                            url: "handler/ExecuteHandler.ashx",                 //把数据提交到ashx  
                            //async: false,
                            data: "sql=" + sqlstring,     //输入框writer中的值作为提交的数据 
                            beforeSend: function () {

                            },
                            success: function (msg) {                  //提交成功后的回调，msg变量是输出的内容。      
                                //如果有必要，可以把msg变量的值显示到某个DIV元素中 
                                $("#" + edit_button + "").css("display", "");
                                $("#" + del_button + "").css("display", "");
                                $("#" + update_button + "").css("display", "none");
                                $("#" + cancel_button + "").css("display", "none");
                                for (r; r < options.tr_count; r++) {
                                    $("#r-" + r + "-" + column + "").css("display", "");
                                    $("#r-" + r + "-" + (Number(column) + 1) + "").css("display", "none");
                                    $("#r-" + r + "-" + (Number(column) + 2) + "").css("display", "none");
                                    $("#r-" + r + "-" + (Number(column) + 3) + "").css("display", "");
                                }
                                for (c; c < column; c++) {
                                    if ($("#r-" + row + "-" + c + "").attr("Dataid") != "time") {//时间字段不需要编辑
                                        var value = $("#r-" + row + "-" + c + " input").val();
                                        $("#r-" + row + "-" + c).attr("Datavalue", value);
                                        $("#r-" + row + "-" + c + " input").remove();
                                        $("#r-" + row + "-" + c + "").append(value);
                                    } else { break; }
                                }
                                parent.layer.msg('处理成功', { icon: 1, time: 1000 });
                                JsonData = {};
                            },
                            error: function (xhr) {
                                if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { // 兼容调试时301/302重定向导致触发error的问题
                                    console.log(xhr);
                                    this.success(eval('(' + xhr.responseText + ')'), xhr.statusText);
                                }
                                parent.layer.msg('处理失败', { icon: 2, time: 1000 });
                            },
                        });
                       
                        // }
                    }
                    else {
                        $("#" + edit_button + "").css("display", "");
                        $("#" + del_button + "").css("display", "");
                        $("#" + update_button + "").css("display", "none");
                        $("#" + cancel_button + "").css("display", "none");
                        for (r; r < options.tr_count; r++) {
                            $("#r-" + r + "-" + column + "").css("display", "");
                            $("#r-" + r + "-" + (Number(column) + 1) + "").css("display", "none");
                            $("#r-" + r + "-" + (Number(column) + 2) + "").css("display", "none");
                            $("#r-" + r + "-" + (Number(column) + 3) + "").css("display", "");
                        }
                        for (c; c < column; c++) {
                            if ($("#r-" + row + "-" + c + "").attr("Dataid") != "time") {//时间字段不需要编辑
                                var value = $("#r-" + row + "-" + c + " input").val();
                                $("#r-" + row + "-" + c).attr("Datavalue", value);
                                $("#r-" + row + "-" + c + " input").remove();
                                $("#r-" + row + "-" + c + "").append(value);
                            } else { break; }
                        }
                        parent.layer.msg('数据没有变动', { icon: 4, time: 1000 });
                    }
                    break;
                case "delete":
                    if (document.getElementById("r-" + row + "-1").innerHTML != "&nbsp;") {//数据是否为空
                        //询问框
                        parent.layer.confirm('您是否要删除？', {
                            btn: ['是的', '算了'] //按钮
                        }, function () {
                            var sql = "delete from " + tableid + " WHERE " + $("#del-" + row).attr("Dataid") + " ='" + $("#del-" + row).attr("Datavalue") + "'";
                            $.ajax({                                                  //调用jquery的ajax方法   
                                type: "POST",                                      //设置ajax方法提交数据的形式   
                                url: "handler/ExecuteHandler.ashx",                 //把数据提交到ashx  
                                //async: false,
                                data: "sql=" + sql,     //输入框writer中的值作为提交的数据 
                                beforeSend: function () {

                                },
                                success: function (msg) {                  //提交成功后的回调，msg变量是输出的内容。      
                                    //如果有必要，可以把msg变量的值显示到某个DIV元素中 
                                    $("#r-" + row).remove();
                                    options.tr_count = $("#cont tr").length;
                                    if (options.tr_count == '0') {
                                        options.curr_num = options.curr_num - 1;
                                        options.curr = options.curr - 1;
                                        options.pagecount = options.pagecount - 1;
                                        plugin_api.GetValue();
                                        setTimeout(plugin_api.paging(), "500");
                                    }
                                    parent.layer.msg('处理成功', { icon: 1, time: 1000 });
                                    JsonData = {};
                                },
                                error: function (xhr) {
                                    if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { // 兼容调试时301/302重定向导致触发error的问题
                                        console.log(xhr);
                                        this.success(eval('(' + xhr.responseText + ')'), xhr.statusText);
                                    }
                                    parent.layer.msg('处理失败', { icon: 2, time: 1000 });
                                },
                            });

                            if (options.original_row != row) {
                                for (a; a < column; a++) {
                                    if (r != row) {
                                        $("#r-" + options.original_row + "-" + column + "").css("display", "");
                                        $("#r-" + options.original_row + "-" + (Number(column) + 1) + "").css("display", "none");
                                        $("#r-" + options.original_row + "-" + (Number(column) + 2) + "").css("display", "none");
                                        $("#r-" + options.original_row + "-" + (Number(column) + 3) + "").css("display", "");
                                    }
                                }
                                for (b; b < column; b++) {
                                    $("#r-" + options.original_row + "-" + b + "").empty();
                                    $("#r-" + options.original_row + "-" + b + "").append($("#r-" + options.original_row + "-" + b + "").attr("Datavalue"));
                                }
                            }

                        }, function () {
                            //layer.msg('取消', {
                            //    time: 20000, //20s后自动关闭
                            //    btn: ['明白了', '知道了']
                            //});
                        });
                    } else { layer.msg('无数据', { icon: 2, time: 2000 }); }
                    break;
                default:
                    //type 与 case 1 和 case 2 不同时执行的代码
            }

        },
        execute: function (table,value,type,filename) {
            var sql;
            switch(type)
            {
                case "insert":
                    sql = "insert into " + table + " values(" + value+ ");"
                    break;
                case "update":
                    sql = "UPDATE " + table + " SET " + value;
                    console.log(sql);
                    break;
                case "delete":

                    break;
                default:
                   // n 与 case 1 和 case 2 不同时执行的代码
            }
            $.ajax({                                                  //调用jquery的ajax方法   
                type: "POST",                                      //设置ajax方法提交数据的形式   
                url: "handler/" + filename + ".ashx",                 //把数据提交到ashx  
                //async: false,
                data: "sql=" + sql,     //输入框writer中的值作为提交的数据 
                beforeSend: function () {

                },
                success: function (msg) {                  //提交成功后的回调，msg变量是输出的内容。      
                    //如果有必要，可以把msg变量的值显示到某个DIV元素中 
                    parent.layer.msg('处理成功', { icon: 1, time: 1000 });
                    JsonData = {};
                },
                error: function (xhr) {
                    if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { // 兼容调试时301/302重定向导致触发error的问题
                        console.log(xhr);
                        this.success(eval('(' + xhr.responseText + ')'), xhr.statusText);
                    }
                    parent.layer.msg('处理失败', { icon: 2, time: 1000 });
                },
            });
            //return result;
        },
        setJson: function (row, name, value) {
            if (EditSingle != row && EditSingle > 0) {
                // 添加 或 修改
                JsonData = {};
                JsonData[name] = value;
                EditSingle = row;
            }
            else {
                // 添加 或 修改
                JsonData[name] = value;
                EditSingle = row;
            }
            console.log(JsonData);
        },
        deleteAll: function () {
            var deleteID = null, rowid = [];
            //options.tr_count = $("#cont tr").length;
            //获取所有选择的列
            $('#cont').children('tr').each(function (idx) {
                var $that = $(this);
                var $cbx = $that.children('td').eq(0).children('input[type=checkbox]')[0].checked;
                if ($cbx) {
                    rowid.push("#r-" + idx);
                    if (deleteID == null) { deleteID = "'" + $("#del-" + idx).attr("datavalue") + "'"; }
                    else { deleteID = deleteID + ",'" + $("#del-" + idx).attr("datavalue") + "'"; }
                }
            });
            
            if (deleteID != null) {
                parent.layer.confirm('您是否要删除？', {
                    btn: ['是的', '算了'] //按钮
                }, function () {
                    var sql = "delete from " + options.table + " where " + $("#selected-all").attr("Dataid") + " in(" + deleteID + ");"
                    $.ajax({                                                  //调用jquery的ajax方法   
                        type: "POST",                                      //设置ajax方法提交数据的形式   
                        url: "handler/ExecuteHandler.ashx",                 //把数据提交到ashx  
                        //async: false,
                        data: "sql=" + sql,     //输入框writer中的值作为提交的数据 
                        beforeSend: function () {

                        },
                        success: function (msg) {                  //提交成功后的回调，msg变量是输出的内容。      
                            //如果有必要，可以把msg变量的值显示到某个DIV元素中 
                            for (var i = 0; i < rowid.length; i++) {
                                $(rowid[i]).remove();
                            }
                            rowid = [];
                            options.tr_count = $("#cont tr").length;
                            if (options.tr_count == '0') {
                                options.curr_num = options.curr_num - 1;
                                options.curr = options.curr - 1;
                                options.pagecount = options.pagecount - 1;
                                plugin_api.GetValue();
                                setTimeout(plugin_api.paging(), "500");
                            }
                            parent.layer.msg('处理成功', { icon: 1, time: 1000 });
                            JsonData = {};
                        },
                        error: function (xhr) {
                            if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { // 兼容调试时301/302重定向导致触发error的问题
                                console.log(xhr);
                                this.success(eval('(' + xhr.responseText + ')'), xhr.statusText);
                            }
                            parent.layer.msg('处理失败', { icon: 2, time: 1000 });
                        },
                    });
                }, function () {
                    //layer.msg('取消', {
                    //    time: 20000, //20s后自动关闭
                    //    btn: ['明白了', '知道了']
                    //});
                });
            }
            else { parent.layer.msg('请先选择要删除的数据！', { icon: 2, time: 2000 }); };
        },
        changeRow: function (row) {
            options.curr = '1';
            options.curr_num = '1';
            if (localStorage.rowlength) { localStorage.rowlength = row; } else { $("#select_row").val("10"); localStorage.rowlength = "10"; }
            plugin_api.GetValue();
            plugin_api.paging();
            
        },
        //添加分类
        addClassRecord: function (v, table) {
            v = "insert into " + table + " values(" + v + ");";
            $.ajax({                                                  //调用jquery的ajax方法   
                type: "POST",                                      //设置ajax方法提交数据的形式   
                url: "handler/ListHTMLhandler.ashx",                                       //把数据提交到ashx  
                data: {
                    "sql": v,
                    "table": table
                },
                dataType: "json",
                beforeSend: function () {
                },
                success: function (msg) {                  //提交成功后的回调，msg变量是输出的内容。      
                    //如果有必要，可以把msg变量的值显示到某个DIV元素中 
                     layer.msg('处理成功', { icon: 1, time: 2000 });
                    // if (table != null) { create(table); }
                },
                error: function (xhr) {
                    if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { // 兼容调试时301/302重定向导致触发error的问题
                        console.log(xhr);
                        this.success(eval('(' + xhr.responseText + ')'), xhr.statusText);
                    }
                    //layer.msg('处理失败', { icon: 2, time: 2000 });
                },
            });
        },
        initKindEditor: function () {
            KindEditor.ready(function (K) {
                editor = K.create('#content1', {
                    cssPath: 'kindeditor/plugins/code/prettify.css',
                    uploadJson: 'kindeditor/upload_json.ashx',
                    fileManagerJson: 'kindeditor/file_manager_json.ashx',
                    resizeType: 1,//不能拖动大小
                    allowFileManager: true,
                    afterCreate: function () {
                        var self = this;
                        self.sync();
                    },
                    //同步KindEditor值
                    afterBlur: function () { this.sync(); }
                });

                K('#J_selectImage').click(function () {
                    editor.loadPlugin('multiimage', function () {
                        editor.plugin.multiImageDialog({
                            clickFn: function (urlList) {
                                var div = K('.gridly');
                                div.html('');
                                var count = $(".gridly").find("div").length;
                                K.each(urlList, function (i, data) {
                                    div.append('<div class="brick small" id=' + Number(i + count) + ' style="position: absolute; left: 0px; top: 0px;"><img index=' + Number(i + count) + ' name="p' + Number(i + count) + '" width="120px" height="120px" src=' + data.url + ' ondblclick="show(this.name);" alt="" /><a class="delete" href="#">×</a></div>')
                                });
                                //生成json  arr[i].id排序索引  
                                editor.hideDialog();
                            }
                        });
                    });
                });
                prettyPrint();
            });
        },
        reLoad: function () {
            var row_count = $("#cont tr").length;
            if (row_count == '0') {
                options.curr_num = options.curr_num - 1;
            } else {
                // CJPlugin.init.
                options.curr_num = $(".layui-laypage-curr").text();
            }
            plugin_api.GetValue();
            plugin_api.paging();
        },
        openWindow: function (type, maxmin, scrollbar, shade, title, area, shadeClose, closeBtn, url, fullscr) {
            var index = parent.layer.open({
                type: type || 1,
                maxmin: maxmin || false,
                scrollbar: scrollbar || false,
                shade: shade || 0,
                title: title || false,
                area: area || 'auto',
                shadeClose: shadeClose || false,
                closeBtn: closeBtn || 0,
                //btn: ['火速围观', '残忍拒绝'],
                //moveType: 1, //拖拽模式，0或者1
                content: "/master/" + url + ".html",
            });
            if (fullscr) { parent.layer.full(index); }
        },
        imageInsert: function () {
            console.log("图片插入中。。。");
        },
        addPhotoClass: function () {
            var v = $("#addvalue").val();
            var d = new Date();
            var t = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes();

            if (v && table != "galleryclass") {
                plugin_api.execute("insert into " + table + " values('" + uuid(6, 16) + "','" + $("#addvalue").val() + "','" + t + "','" + table + "');", table);

            } else {
                plugin_api.execute("insert into " + table + " values('" + uuid(6, 16) + "','" + $("#addvalue").val() + "','" + t + "');", null);
            }
        }
    }
    //这里确定了插件的名称
    this.CJPlugin = plugin_api;
})(jQuery);



    //adduser:function(tableid) {
    //    $("#addbtn").attr("disabled", "disabled");
    //    $("#addbtn").attr('value', "loading");
    //    setTimeout(function () {
    //        $("#addbtn").attr('value', "添加");
    //        $("#addbtn").removeAttr("disabled");
    //    }, 2000);
    //    var d = new Date();
    //    var t = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes();
    //    var value = "'" + uuid(8, 16) + "','" + $("#username").val() + "','" + $("#password").val() + "','" + $("#email").val() + "','" + t + "'";
    //    var sqlstring = "insert into " + tableid + " values(" + value + ");"
    //    execute(sqlstring)
    //},





